<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>svm2.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model&nbsp;</span>=&nbsp;<span class=defun_name>svm2</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;SVM2&nbsp;Learning&nbsp;of&nbsp;binary&nbsp;SVM&nbsp;classifier&nbsp;with&nbsp;L2-soft&nbsp;margin.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm2(data)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm2(data,options)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;learns&nbsp;binary&nbsp;Support&nbsp;Vector&nbsp;Machines</span><br>
<span class=help>%&nbsp;&nbsp;classifier&nbsp;with&nbsp;L2-soft&nbsp;margin.&nbsp;The&nbsp;corresponding&nbsp;quadratic&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;programming&nbsp;task&nbsp;is&nbsp;solved&nbsp;by&nbsp;one&nbsp;of&nbsp;the&nbsp;following&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;<span class=help_field>algorithms:</span></span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;mdm&nbsp;&nbsp;...&nbsp;Mitchell-Demyanov-Malozemov&nbsp;(MDM)&nbsp;algorithm.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;imdm&nbsp;...&nbsp;Improved&nbsp;MDM&nbsp;algorithm&nbsp;(IMDM)&nbsp;(defaut).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;For&nbsp;more&nbsp;info&nbsp;refer&nbsp;to&nbsp;V.Franc:&nbsp;Optimization&nbsp;Algorithms&nbsp;for&nbsp;Kernel&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;Methods.&nbsp;Research&nbsp;report.&nbsp;CTU-CMP-2005-22.&nbsp;CTU&nbsp;FEL&nbsp;Prague.&nbsp;2005.</span><br>
<span class=help>%&nbsp;&nbsp;ftp://cmp.felk.cvut.cz/pub/cmp/articles/franc/Franc-PhD.pdf&nbsp;.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Training&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;must&nbsp;equal&nbsp;1&nbsp;and/or&nbsp;2.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.ker&nbsp;[string]&nbsp;Kernel&nbsp;identifier.&nbsp;See&nbsp;'help&nbsp;kernel'.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.arg&nbsp;[1&nbsp;x&nbsp;nargs]&nbsp;Kernel&nbsp;argument(s).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.C&nbsp;[1x2]&nbsp;Regularization&nbsp;constants&nbsp;for&nbsp;class&nbsp;1&nbsp;and&nbsp;2;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;C&nbsp;is&nbsp;[1x1]&nbsp;then&nbsp;the&nbsp;same&nbsp;C&nbsp;is&nbsp;used&nbsp;for&nbsp;both&nbsp;classes.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.solver&nbsp;[string]&nbsp;Solver&nbsp;to&nbsp;be&nbsp;used:&nbsp;'mdm',&nbsp;'imdm'&nbsp;(default).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.tmax&nbsp;[1x1]&nbsp;Maximal&nbsp;number&nbsp;of&nbsp;iterations&nbsp;(default&nbsp;inf).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.tolabs&nbsp;[1x1]&nbsp;Absolute&nbsp;tolerance&nbsp;stopping&nbsp;condition&nbsp;(default&nbsp;0.0).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.tolrel&nbsp;[1x1]&nbsp;Relative&nbsp;tolerance&nbsp;stopping&nbsp;condition&nbsp;(default&nbsp;1e-3).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.thlb&nbsp;[1x1]&nbsp;Threshold&nbsp;on&nbsp;lower&nbsp;bound&nbsp;(default&nbsp;inf).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.cache&nbsp;[1x1]&nbsp;#of&nbsp;columns&nbsp;of&nbsp;kernel&nbsp;matrix&nbsp;to&nbsp;be&nbsp;cached&nbsp;(default&nbsp;1000).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.verb&nbsp;[1x1]&nbsp;If&nbsp;&gt;&nbsp;0&nbsp;then&nbsp;some&nbsp;info&nbsp;is&nbsp;displayed&nbsp;(default&nbsp;0).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Binary&nbsp;SVM&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;1]&nbsp;Weights&nbsp;of&nbsp;support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias&nbsp;of&nbsp;decision&nbsp;function.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;nsv]&nbsp;Support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.inx&nbsp;[1&nbsp;x&nbsp;nsv]&nbsp;Indices&nbsp;of&nbsp;SVs&nbsp;(model.sv.X&nbsp;=&nbsp;data.X(:,inx)).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[int]&nbsp;Number&nbsp;of&nbsp;Support&nbsp;Vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.trnerr&nbsp;[1x1]&nbsp;Classification&nbsp;error&nbsp;on&nbsp;training&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.margin&nbsp;[1x1]&nbsp;Margin.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options&nbsp;[struct]&nbsp;Copy&nbsp;of&nbsp;used&nbsp;options.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.cputime&nbsp;[1x1]&nbsp;Used&nbsp;CPU&nbsp;time&nbsp;in&nbsp;seconds&nbsp;(meassured&nbsp;by&nbsp;tic-toc).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.stat&nbsp;[struct]&nbsp;Statistics&nbsp;about&nbsp;optimization:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.access&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;requested&nbsp;columns&nbsp;of&nbsp;matrix&nbsp;H.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.t&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;iterations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.UB&nbsp;[1x1]&nbsp;Upper&nbsp;bound&nbsp;on&nbsp;the&nbsp;optimal&nbsp;value&nbsp;of&nbsp;criterion.&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LB&nbsp;[1x1]&nbsp;Lower&nbsp;bound&nbsp;on&nbsp;the&nbsp;optimal&nbsp;value&nbsp;of&nbsp;criterion.&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LB_History&nbsp;[1x(t+1)]&nbsp;LB&nbsp;with&nbsp;respect&nbsp;to&nbsp;iteration.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.UB_History&nbsp;[1x(t+1)]&nbsp;UB&nbsp;with&nbsp;respect&nbsp;to&nbsp;iteration.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.NA&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;non-zero&nbsp;entries&nbsp;in&nbsp;solution.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;=&nbsp;struct('ker','rbf','arg',1,'C',1);</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm2(data,options&nbsp;)</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;ppatterns(data);&nbsp;psvm(&nbsp;model&nbsp;);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also</span><br>
<span class=help>%&nbsp;&nbsp;SVMCLASS,&nbsp;SVMLIGHT,&nbsp;SMO,&nbsp;GNPP.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2005,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;07-sep-2007,&nbsp;VF,&nbsp;now&nbsp;it&nbsp;is&nbsp;possible&nbsp;to&nbsp;use&nbsp;distinct&nbsp;reg.&nbsp;constants&nbsp;for&nbsp;both&nbsp;classes</span><br>
<span class=help1>%&nbsp;09-sep-2005,&nbsp;VF</span><br>
<span class=help1>%&nbsp;08-aug-2005,&nbsp;VF</span><br>
<span class=help1>%&nbsp;24-jan-2005,&nbsp;VF</span><br>
<span class=help1>%&nbsp;29-nov-2004,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;restart&nbsp;clock</span><br>
tic;<br>
<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options&nbsp;=&nbsp;c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'solver'</span>),&nbsp;options.solver&nbsp;=&nbsp;<span class=quotes>'imdm'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'tolabs'</span>),&nbsp;options.tolabs&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'tolrel'</span>),&nbsp;options.tolrel&nbsp;=&nbsp;1e-3;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'thlb'</span>),&nbsp;options.thlb&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'tmax'</span>),&nbsp;options.tmax&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'C'</span>),&nbsp;options.C&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;length(options.C)==1,&nbsp;options.C&nbsp;=&nbsp;[1&nbsp;1]*options.C;&nbsp;<span class=keyword>end</span>&nbsp;<span class=comment>%&nbsp;C&nbsp;[1&nbsp;x&nbsp;2]&nbsp;</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'ker'</span>),&nbsp;options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'arg'</span>),&nbsp;options.arg&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'cache'</span>),&nbsp;options.cache&nbsp;=&nbsp;1000;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'verb'</span>),&nbsp;options.verb&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;call&nbsp;MEX&nbsp;implementation&nbsp;of&nbsp;QPC2&nbsp;solver</span><br>
[Alpha,b,exitflag,kercnt,access,errcnt,t,UB,LB,History]&nbsp;=&nbsp;svm2_mex(...<br>
&nbsp;&nbsp;&nbsp;&nbsp;data.X,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;data.y,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.ker,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.arg,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.C,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.solver,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.tmax,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.tolabs,&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.tolrel,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.thlb,&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.cache,&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.verb&nbsp;);<br>
<br>
<span class=comment>%&nbsp;remove&nbsp;non-support&nbsp;vectors</span><br>
inx&nbsp;=&nbsp;find(Alpha&nbsp;~=0&nbsp;);<br>
<br>
<span class=comment>%&nbsp;setup&nbsp;output&nbsp;model</span><br>
model.Alpha&nbsp;=&nbsp;Alpha(inx);<br>
model.b&nbsp;=&nbsp;b;<br>
model.sv.X&nbsp;=&nbsp;data.X(:,inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.sv.y&nbsp;=&nbsp;data.y(inx);<br>
model.nsv&nbsp;=&nbsp;length(inx);<br>
<span class=keyword>if</span>&nbsp;strcmp(&nbsp;options.ker,&nbsp;<span class=quotes>'linear'</span>),<br>
&nbsp;&nbsp;model.W&nbsp;=&nbsp;model.sv.X&nbsp;*&nbsp;model.Alpha;<br>
<span class=keyword>end</span><br>
model.options&nbsp;=&nbsp;options;<br>
model.kercnt&nbsp;=&nbsp;kercnt;<br>
model.trnerr&nbsp;=&nbsp;errcnt/size(data.X,2);<br>
model.errcnt&nbsp;=&nbsp;errcnt;<br>
<span class=comment>%model.margin&nbsp;=&nbsp;1/sqrt(sum(abs(model.Alpha))-sum(model.Alpha.^2)...</span><br>
<span class=comment>%&nbsp;&nbsp;&nbsp;&nbsp;/2/model.options.C);</span><br>
model.exitflag&nbsp;=&nbsp;exitflag;<br>
model.stat.access&nbsp;=&nbsp;access;<br>
model.stat.t&nbsp;=&nbsp;t;<br>
model.stat.UB&nbsp;=&nbsp;UB;<br>
model.stat.LB&nbsp;=&nbsp;LB;<br>
model.stat.LB_History&nbsp;=&nbsp;History(1,:);<br>
model.stat.UB_History&nbsp;=&nbsp;History(2,:);<br>
model.stat.NA&nbsp;=&nbsp;length(inx);<br>
model.cputime&nbsp;=&nbsp;toc;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'svmclass'</span>;<br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%EOF</span><br>
<br>
</code>
